<?php

namespace App\Admin\Controllers;

use App\Models\Department;
use App\Models\Management;
use App\Models\Mechanism;
use App\Models\Post;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;

class ManagementController extends Controller
{
    use HasResourceActions;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('编辑');
            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header('新建');
            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Management);
        $grid->actions(function ($actions) {
            $actions->disableView();
        });
        $grid->disableExport();
        $grid->id('ID')->sortable();
        $grid->no('编号');
        $grid->mechanism('机构')->using(Mechanism::pluck('title','id')->toArray());
        $grid->department('部门')->using(Department::pluck('title','id')->toArray());
        $grid->post('岗位')->using(Post::pluck('title','id')->toArray());
        $grid->name('姓名');
        $grid->sex('性别')->using(config('options.sex'));
        $grid->email('邮箱');
        $grid->phone('电话');
        $grid->duty('职责');
        return $grid;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Management);
        $form->text('no','编号');
        $form->select('mechanism', '机构')->options(Mechanism::pluck('title','id'))->load('department', '/api/manager/department');
        $form->select('department', '部门')->options(Department::pluck('title','id'))->load('post', '/api/manager/post');;
        $form->select('post', '岗位')->options(Post::pluck('title','id'));
        $form->text('name','姓名');
        $form->radio('sex', '性别')->options(config('options.sex'));
        $form->email('email','邮箱')->rules('',['email'   => '请输入正确的email格式']);
        $form->text('phone','电话');
        $form->text('duty','职责');
        return $form;


    }
}
